package com.ziyun.erp.web.api.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.e.service.ServiceTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;
import com.ziyun.erp.modules.material.entity.ErpStockEntity;
import com.ziyun.erp.modules.material.service.ErpStockService;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.production.service.ErpOrderItemService;
import com.ziyun.erp.modules.service.entity.ServiceReceiveGoodsEntity;
import com.ziyun.erp.modules.service.entity.ServiceRefundsEntity;
import com.ziyun.erp.modules.service.entity.ServiceRegisterEntity;
import com.ziyun.erp.modules.service.service.ServiceReceiveGoodsService;
import com.ziyun.erp.modules.service.service.ServiceRegisterService;
import com.ziyun.erp.modules.service.vo.ServiceOrderInfoVo;
import com.ziyun.erp.modules.service.vo.ServiceRegisterVo;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 售后登记表
 *
 * @author llc
 * @email 787254039@qq.com
 * @date
 */
@RestController
@RequestMapping("port/service/serviceregister")
public class ServiceRegisterControllerApi {
    @Autowired
    private ServiceRegisterService serviceRegisterService;

    @Autowired
    private ErpOrderService orderService;

    @Autowired
    private ErpOrderItemService orderItemService;

    @Autowired
    private SdbEctoolsRegionsService sdbEctoolsRegionsService;

    @Autowired
    private ErpStockService erpStockService;

    @Autowired
    private ServiceReceiveGoodsService serviceReceiveGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("service:serviceregister:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = serviceRegisterService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{srId}")
    @RequiresPermissions("service:serviceregister:info")
    public R info(@PathVariable("srId") Integer srId){
        ServiceRegisterEntity serviceRegister = serviceRegisterService.selectById(srId);
        ServiceOrderInfoVo serviceOrderInfoVo = serviceRegisterService.selectServiceOrderInfo(serviceRegister.getSrOrderId());

        //查询售后商品明细
        if(serviceRegister.getSrType().intValue() ==ServiceTypeEnum.SERVICE_RETURN_REFUND.toInt() ||
                serviceRegister.getSrType().intValue() ==ServiceTypeEnum.SERVICE_EXCHANGE_GOODS.toInt()){
            //涉及退换货才有商品明细
            List<Map<String,Object>> mapsOrderItem  = this.serviceReceiveGoodsService.selectMaps(new EntityWrapper<ServiceReceiveGoodsEntity>()
                    .setSqlSelect("stock_id as stockId, stock_name as stockName")
                    .eq("status", 0)
                    .eq("parent_id", 2));
        }


        return R.ok().put("serviceRegister", serviceRegister).
                put("serviceOrderInfoVo", serviceOrderInfoVo);
    }

    /**
     * 售后登记
     * @param
     * @return
     */
    @RequestMapping("/getRegisterInfo")
    public R getRegisterInfo(@RequestBody Map<String, Object> paramBodyMap){
        Long orderId=Long.valueOf(paramBodyMap.get("orderId").toString());
        //查询该订单信息
        ErpOrderEntity erpOrderEntity =  orderService.selectByOrderId(orderId);
        if (erpOrderEntity == null){
            throw new RRException("未查询到该订单!");
        }
        // 查询该订单明细
        List<ErpOrderItemEntity> orderItemList = orderItemService.selectList(new EntityWrapper<ErpOrderItemEntity>().eq("order_no",erpOrderEntity.getOrderNo()));
        if (orderItemList == null || orderItemList.size()==0){
            throw new RRException("未查询到该订单明细!");
        }

        //存放枚举类
        Map<String, List> mapEnum = new HashMap<>();
        mapEnum.put("serviceTypeEnum", JsonUtils.enumToList(ServiceTypeEnum.class));
        //ServiceRegisterEntity serviceRegister = serviceRegisterService.selectById(srId);

        // 查询省市区级联数据
        List<String> regionsSascade = sdbEctoolsRegionsService.queryRegionsSascade();

        //查询售后仓库列表
        List<Map<String, Object>> srStockList = erpStockService.selectMaps(new EntityWrapper<ErpStockEntity>()
                                    .setSqlSelect("stock_id as stockId, stock_name as stockName")
                                    .eq("status", 0)
                                    .eq("parent_id", 2)
                                    );

        ServiceRegisterEntity serviceRegister = new ServiceRegisterEntity();
        serviceRegister.setSrOrderId(erpOrderEntity.getOrderNo());
        serviceRegister.setSrType(1);
        serviceRegister.setSrName(erpOrderEntity.getMemberName());

        ServiceRefundsEntity serviceRefunds = new ServiceRefundsEntity();
        serviceRefunds.setSrRefundPath(1);

        return R.ok().put("erpOrderInfo",erpOrderEntity).
                put("erpOrderItemList", orderItemList).
                put("regionsSascade", JSONArray.fromObject(regionsSascade).toString()).
                put("srStockList", srStockList).
                put("mapEnum",mapEnum).
                put("serviceRegister", serviceRegister).
                put("serviceRefunds", serviceRefunds);

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("service:serviceregister:save")
    public R save(@Valid @RequestBody ServiceRegisterVo serviceRegister){
        serviceRegisterService.saveRegister(serviceRegister);
        return R.ok();
    }

    /**
     * 添加售后责任
     * @param serviceRegister
     * @return
     */
    @RequestMapping("/addResponsibility")
    public R addResponsibility(@RequestBody ServiceRegisterEntity serviceRegister){
        serviceRegisterService.updateById(serviceRegister);
        return R.ok();
    }

    /**
     * 审核
     * @param serviceRegister
     * @return
     */
    @RequestMapping("/check")
    public R check(@RequestBody ServiceRegisterEntity serviceRegister){
        serviceRegisterService.check(serviceRegister);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("service:serviceregister:update")
    public R update(@RequestBody ServiceRegisterEntity serviceRegister){
        ValidatorUtils.validateEntity(serviceRegister);
        serviceRegisterService.updateAllColumnById(serviceRegister);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("service:serviceregister:delete")
    public R delete(@RequestBody Integer[] srIds){
        serviceRegisterService.deleteBatchIds(Arrays.asList(srIds));

        return R.ok();
    }

}
